/**
 * 
 */
package cn.ivbao.daos;

/**
 * @author wenming Mar 26, 2008
 */
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ReflectUtil
{

  private static final Log logger = LogFactory.getLog(ReflectUtil.class);

  public static void setFieldValue(Object target, String fname, Class ftype,
      Object fvalue)
  {
    if (target == null || fname == null || "".equals(fname)
        || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass())))
    {
      return;
    }
    Class clazz = target.getClass();
    try
    {
      Method method = clazz.getDeclaredMethod("set"
          + Character.toUpperCase(fname.charAt(0)) + fname.substring(1),
          new Class[]{ftype});
      if (!Modifier.isPublic(method.getModifiers()))
      {
        method.setAccessible(true);
      }
      method.invoke(target, new Object[]{fvalue});
    }
    catch (Exception me)
    {
      if (logger.isDebugEnabled())
      {
        logger.debug(me);
      }
      try
      {
        Field field = clazz.getDeclaredField(fname);
        if (!Modifier.isPublic(field.getModifiers()))
        {
          field.setAccessible(true);
        }
        field.set(target, fvalue);
      }
      catch (Exception fe)
      {
        if (logger.isDebugEnabled())
        {
          logger.debug(fe);
        }
      }
    }
  }

  /**
   * description
   * 
   * @param type
   * @return
   */
  public static String getOperateType(String type, String dbType)
  {
    if (type.equalsIgnoreCase("SELECT"))
    {
      return "'SELECT'";
    }
    else if (type.equalsIgnoreCase("VIEW"))
    {
      return "'VIEW'";
    }
    else if (type.equalsIgnoreCase("INSERT"))
    {
      return "'INSERT','DML'";
    }
    else if (type.equalsIgnoreCase("UPDATE"))
    {
      return "'UPDATE','DML'";
    }
    else if (type.equalsIgnoreCase("DELETE"))
    {
      return "'DELETE','DML'";
    }
    else
    {
      return "'SELECT'";
    }
  }
}
